﻿using DevExpress.XtraEditors;
using DXApplicationTangche.UC.圣同润.merchant;
using DXApplicationTangche.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 广州爱特安为科技.UC.圣同润.客服
{
    public partial class Form拆单备注导入 : DevExpress.XtraEditors.XtraForm
    {

        private List<RemarkBodyVO> remarkBodyList = new List<RemarkBodyVO>();
        public Form拆单备注导入()
        {
            InitializeComponent();
        }

        private void simpleButton添加插单备注数据_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = MIMEConst.EXCEL_MIME;
            openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            openFile.Multiselect = false;
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                DataSet result = ExcelUtil.getExcelDatas(openFile.FileName);
                if (result == null)
                {
                    return;
                }
                DataTable dataTable = result.Tables[0];

                this.remarkBodyList.Clear();
                int i = 0;
                foreach (DataRow dataRow in dataTable.Rows)
                {
                    if (i != 0)
                    {
                        RemarkBodyVO remarkBodyVO = new RemarkBodyVO(dataRow);
                        if (!String.IsNullOrEmpty(remarkBodyVO.delivery_id))
                            this.remarkBodyList.Add(remarkBodyVO);
                    }

                    i++;
                }
                this.gridControl备注.DataSource = this.remarkBodyList;
                this.gridView备注.RefreshData();
            }
            this.simpleButton提交插单备注数据.Enabled = true;
        }

        private void simpleButton提交插单备注数据_Click(object sender, EventArgs e)
        {
            if (this.gridView备注.DataRowCount <= 0)
            {
                return;
            }
            List<RemarkBody> remarkBody = new List<RemarkBody>();
            foreach (RemarkBodyVO item in this.remarkBodyList)
            {
                remarkBody.Add(new RemarkBody(item));
            }
            if (remarkBody.Count > 0)
            {
                //  批量拆单备注导入
                MerchantService.batchUpdateRemark(remarkBody);
                MessageBox.Show("操作结束");
                //  刷新
                this.remarkBodyList = new List<RemarkBodyVO>();
                this.gridControl备注.DataSource = this.remarkBodyList;
                this.gridView备注.RefreshData();
            }
        }
    }

    public class RemarkBodyVO
    {
        public String order_sn { get; set; }
        public String delivery_name { get; set; }
        public String delivery_id { get; set; }
        public String remark { get; set; }

        public RemarkBodyVO(DataRow dataRow)
        {
            this.order_sn = dataRow[0].ToString();
            this.delivery_name = dataRow[1].ToString();
            this.delivery_id = dataRow[2].ToString();
            this.remark = this.delivery_name + " " + this.delivery_id;
        }
    }
}